﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Otherlist_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            getNgonngu()
            If Not Request.QueryString("Type").ToString.Equals("0") Then
                hidType.Value = Request.QueryString("Type")
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If

        End If
    End Sub
    Protected Sub getNgonngu()
        ddlLanguageId.Items.Clear()
        ddlLanguageId.AppendDataBoundItems = True
        ddlLanguageId.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.Languages Select q.LanguageId, q.Name).ToList
            ddlLanguageId.DataValueField = "LanguageId"
            ddlLanguageId.DataTextField = "Name"
            ddlLanguageId.DataSource = p
            ddlLanguageId.DataBind()
            ddlLanguageId.SelectedValue = 0
        End Using
    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As OtherList = (From q In data.OtherLists Where q.Type = hidType.Value AndAlso q.Id = hidID.Value Select q).FirstOrDefault

            If Not p Is Nothing Then
                ddlDanhMucType.SelectedValue = IIf(IsNothing(p.Type) = True, "", p.Type)
                Dim _othersTable = (From q In data.OtherListTables Where q.OLCode = p.Type Select q).FirstOrDefault
                ddlLanguageId.SelectedValue = _othersTable.LanguageId

 
                txtPhanloai.Text = IIf(IsNothing(p.PhanLoai) = True, "", p.PhanLoai)

                txtPhanloai.Text = IIf(IsNothing(p.PhanLoai) = True, "", p.PhanLoai)
                'txtHinhdo.Text = IIf(IsNothing(p.HinhDo) = True, "", p.HinhDo)
                txtTucanhbao.Text = IIf(IsNothing(p.TuCanhBao) = True, "", p.TuCanhBao)
                txtCanhbaonguyco.Text = IIf(IsNothing(p.CanhBaoNguyCo) = True, "", p.CanhBaoNguyCo.Trim)
                'for type
                Dim h = (From q In data.OtherListTables Where q.LanguageId = ddlLanguageId.SelectedValue Select q.OLCode, q.OtherListName).ToList
                ddlDanhMucType.DataValueField = "OLCode"
                ddlDanhMucType.DataTextField = "OtherListName"
                ddlDanhMucType.DataSource = h
                ddlDanhMucType.DataBind()
                hplSelKeyWord.CssClass = "thickbox"
                hplSelKeyWord.NavigateUrl = "~/Page/HinhDo/HinhDo.aspx?LanguageId=" & ddlLanguageId.SelectedValue & "&keepThis=true&TB_iframe=true&height=450&width=500&modal=true"

                ddlDanhMucType.SelectedValue = p.Type

                'Show keyword
                Dim obj = (From ck In data.OtherListHinhDoes _
                           Join k In data.HinhDoes On ck.HinhDoId Equals k.HinhDoId _
                                Where ck.OtherListType = p.Type AndAlso ck.OtherListId = p.Id Order By k.HinhDoCode Ascending Select New With {.Name = k.HinhDoCode & " - " & k.TenGoi, .Value = k.HinhDoId}).ToList
                For i As Integer = 0 To obj.Count - 1
                    txtKeyword.Text &= obj(i).Value & ";"
                    txtStrKeyWord.Text &= obj(i).Name & ";"
                Next
                Excute_Javascript("appendOptionLast('" & txtKeyword.Text & "','" & txtStrKeyWord.Text & "','selKeyWord');", Me.Page, True)


            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As OtherList = (From q In data.OtherLists Where q.Type = hidType.Value AndAlso q.Id = hidID.Value Select q).FirstOrDefault
            Try

                p.PhanLoai = txtPhanloai.Text.Trim()
                p.Name = txtPhanloai.Text.Trim()
                '  p.HinhDo = txtHinhdo.Text.Trim()
                p.TuCanhBao = txtTucanhbao.Text.Trim()
                p.CanhBaoNguyCo = txtCanhbaonguyco.Text.Trim()
                data.SaveChanges()

                'delete hinh do 
                Dim obj = (From ck In data.OtherListHinhDoes
                              Where ck.OtherListType = p.Type AndAlso ck.OtherListId = p.Id Select ck).ToList

                For Each a In obj
                    data.OtherListHinhDoes.DeleteObject(a)
                    data.SaveChanges()
                Next

                'Inser OtherListHinhDo
                Dim arrKeyWord As String() = txtKeyword.Text.TrimEnd(";").Split(";")
                For i As Integer = 0 To arrKeyWord.Length - 1
                    If arrKeyWord(i) <> "" Then
                        Dim q As New OtherListHinhDo
                        q.OtherListType = p.Type
                        q.OtherListId = p.Id
                        q.HinhDoId = arrKeyWord(i)
                        'q.ContentId = iContentID
                        'q.KeywordId = arrKeyWord(i)
                        data.OtherListHinhDoes.AddObject(q)
                        data.SaveChanges()
                    End If

                Next



                Insert_App_Log("update Otherlist:" & txtPhanloai.Text.Trim & "", Function_Name.OtherList, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Otherlist/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub

    Protected Sub ddlLanguageId_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlLanguageId.SelectedIndexChanged
        Using data As New VSEntities
            Dim p = (From q In data.OtherListTables Where q.LanguageId = ddlLanguageId.SelectedValue Select q.OLCode, q.OtherListName).ToList
            ddlDanhMucType.DataValueField = "OLCode"
            ddlDanhMucType.DataTextField = "OtherListName"
            ddlDanhMucType.DataSource = p
            ddlDanhMucType.DataBind()
            hplSelKeyWord.CssClass = "thickbox"
            hplSelKeyWord.NavigateUrl = "~/Page/HinhDo/HinhDo.aspx?LanguageId=" & ddlLanguageId.SelectedValue & "&keepThis=true&TB_iframe=true&height=450&width=500&modal=true"
        End Using
    End Sub

#End Region
End Class
